<template>
  <Form ref="loginForm" :model="form" :rules="rules" @keydown.enter.native="handleSubmit">
    <FormItem prop="userName">
      <Input v-model="form.userName" :placeholder="$t('components.loginForm.enterUserName')">
        <span slot="prepend">
          <Icon :size="16" type="ios-person"></Icon>
        </span>
      </Input>
    </FormItem>
    <FormItem prop="password">
      <Input type="password" v-model="form.password" :placeholder="$t('components.loginForm.enterPassword')">
        <span slot="prepend">
          <Icon :size="14" type="md-lock"></Icon>
        </span>
      </Input>
    </FormItem>
    <FormItem>
      <Button @click="handleSubmit" class="btnColor" long>{{ $t('components.loginForm.login') }}</Button>
    </FormItem>
  </Form>
</template>
<script>
export default {
  name: 'LoginForm',
  props: {
    userNameRules: {
      type: Array,
      default: function() {
        return [
          { required: true, message: this.$i18n.tc('components.loginForm.accountNotNone'), trigger: 'blur' }
        ]
      }
    },
    passwordRules: {
      type: Array,
      default: function() {
        return [
          { required: true, message: this.$i18n.tc('components.loginForm.passwordNotNone'), trigger: 'blur' }
        ]
      }
    }
  },
  data () {
    return {
      form: {
        // userName: 'pengjing',
        userName: process.env.NODE_ENV === 'development' ? 'test' : '',
        password: process.env.NODE_ENV === 'development' ? '123456' : '',
      }
    }
  },
  computed: {
    rules () {
      return {
        userName: this.userNameRules,
        password: this.passwordRules
      }
    }
  },
  methods: {
    handleSubmit () {

      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.$emit('on-success-valid', {
            userName: this.form.userName,
            password: this.form.password
          })
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
  @function r($px) {
    @return $px/100*1rem;
  }
  .btnColor{
    background: #ff802c;
    color: #fff;
    border-color: #ff802c;


  }
</style>
